import { createRouter, createWebHashHistory } from 'vue-router'
const router = createRouter({
    history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
    routes: [
        {
            path: '/',
            redirect: '/Layout'
        },
        {
            path: '/Layout',
            name: 'layout',
            component: () => import(/* webpackChunkName: "introduce" */ '@/views/Layout/index.vue'),
            redirect: '/Layout/Home',//重定向到二级路由Home
            children: [
                {
                    path: 'Search',
                    name: 'search',
                    component: () => import(/* webpackChunkName: "introduce" */ '@/views/Search/index.vue')
                },
                {
                    path: 'Home',
                    name: 'home',
                    component: () => import(/* webpackChunkName: "introduce" */ '@/views/Home/index.vue')
                }
            ]
        },
        {
            path: '/Play',
            name: 'play',
            component: () => import(/* webpackChunkName: "introduce" */ '@/views/Play/index.vue')
        },
    ]
})
// 配置 keep-alive 组件
router.beforeEach((to, from, next) => {
    const keepAlive = to.meta.keepAlive;
    if (keepAlive) {
        next();
    } else {
        const includes = ["search"]; // 需要缓存的组件
        const excludes = ["home", "play"]; // 不需要缓存的组件
        if (includes.indexOf(to.name) !== -1) {
            to.meta.keepAlive = true;
        } else if (excludes.indexOf(to.name) !== -1) {
            to.meta.keepAlive = false;
        }
        next();
    }
});
export default router